import {Article as articleModol, Types as typesModol} from '../../modol/Article'
import unifyResponse from '../../methods/responseConfig'


class Article{
    
    constructor(){}

    async addArticle(req, res, next){
    	try{
            if(!req.body.title)
            throw new Error('标题不能为空');
            else if(!req.body.url)	
            throw new Error('url不能为空');
            else if(!req.body.type)
            throw new Error('文章类型不能为空');
            else if(!req.body.description)
            throw new Error('文章描述不能为空');
    	}
      catch(err){
            console.log(err.message, err,"hahahaha");
            unifyResponse(res,0,'GET_ADDARTICLEPARAM_ERROR',err.message);
		        return 
    	}
    	try{
           const article={
           	  Article_title: req.body.title,
           	  Article_url: req.body.url,
              Article_sort: Number(req.body.sort) || 0 ,
              Article_content: req.body.content || '' ,
              Article_type: req.body.type,
              Article_imgUrl: req.body.imgUrl || 'material-1.png',
              Article_description: req.body.description,
              Article_preContent: req.body.preContent
           }
           await articleModol.create(article);
           console.log('插入成功');
           unifyResponse(res,1,'INSERT_Success','插入成功');
    	}catch(err){
           console.log(err);
           unifyResponse(res,0,'INSERT_ERROR_PARAM','插入错误');
           return 
    	}
    }
  
    async addTypes(req, res, next){
        try
        {
           if(!req.body.type||req.body.type=='')
           throw new Error('文章类型不能为空');
        }
        catch(err)
        {
           console.log(err);
           unifyResponse(res,0,'GET_ERROR_TYPES',err.message);
           return 
        }
        try
        {
             const type={
               Types_name: req.body.type,
             }
             await typesModol.create(type);
             console.log('插入成功');
             unifyResponse(res,1,'INSERT_SUCCESS','插入成功');
        }
        catch(err)
        {
             console.log(err);
             unifyResponse(res,0,'INSERT_ERROR_TYPES','插入错误');
             return 
        }
    }

    async deleteType(req, res, next){
        try
        {
           if(!req.body.type||req.body.type=='')
           throw new Error('文章类型不能为空');
        }
        catch(err)
        {
           console.log(err);
           unifyResponse(res,0,'GET_ERROR_TYPES',err.message);
           return 
        }
        try
        {
             const type={
               Types_name: req.body.type,
             }
             await typesModol.remove(type);
             await articleModol.remove({Article_type:req.body.type});
             console.log('删除类型成功');

             unifyResponse(res,1,'INSERT_SUCCESS','删除类型成功');
        }
        catch(err)
        {
             console.log(err);
             unifyResponse(res,0,'INSERT_ERROR_TYPES','删除类型失败');
             return 
        }
    }

    async findAllTypes(req, res, next){
         try
         {
              let data = await typesModol.find({});
              unifyResponse(res,1,'SEARCH_SUCCESS',data);
         }
         catch(err)
         {
              console.log(err);
              unifyResponse(res,0,'SEARCH_ERROR','查找类型失败');
              return 
         }
    }

    async selectDetailByPage(req, res, next)
    {
          let page;
          if(!req.params.page||req.params.page=='')
             page=0;
          else
          {
             if(Number(req.params.page)>0)
             page=Number(req.params.page)-1;
             else
             throw new Error('文章描述不能为空');
             console.log(page+'-------');   
          } 
          try
          {
              if(page==0)
              res.redirect('/');
              let data = await articleModol.find({},{"_id":0,"__v":0,"Article_sort":0,"Article_content":0})
              .sort({"Article_sort":-1,"_id":-1}).skip(page*5).limit(5);
              let allNumber=await articleModol.count();
              let types = await typesModol.find({},{"_id":0,"__v":0});
              let leftPage=page,rightPage;
              if(allNumber>page*5+5)
                rightPage=leftPage+2;
              else
                rightPage=-1;
              res.render('index',{articles: data, page: page+1, leftPage: leftPage, rightPage: rightPage, types: types});
          }
          catch(err)
          {
              console.log(err);
              unifyResponse(res,0,'SEARCH_ARTICLE_ERROR','查找文章信息失败');
              return 
          }
    }

    async selectDetail(req, res, next)
    {
         try
          {
              let data = await articleModol.find({},{"_id":0,"__v":0,"Article_sort":0,"Article_content":0})
              .sort({"Article_sort":-1,"_id":-1}).limit(5);
              //console.log(data);
              let allNumber=await articleModol.count();
              let types = await typesModol.find({},{"_id":0,"__v":0});
              let leftPage=-1,rightPage;
              if(allNumber>5)
              rightPage=2
              else
              rightPage=-1;
              res.render('index',{articles: data, page: 1, leftPage: leftPage, rightPage: rightPage, types: types});
          }
          catch(err)
          {
              console.log(err);
              unifyResponse(res,0,'GET_INDEXPAGE_ERROR','获取文章首页失败');
              return 
          }
    }

    async displayArticle(req, res, next)
    {
         try
         {
             if(!req.params.url)
             throw new Error('获取文章url失败');
             let data = await articleModol.findOne({Article_url: req.params.url},{"_id":0,"__v":0,"Article_sort":0,"Article_preContent":0,"Article_url":0,"Article_type":0});
             let types = await typesModol.find({},{"_id":0,"__v":0});
             res.render('article',{articleDetail: data, types: types});
         }
         catch(err)
         {
             console.log(err);
             unifyResponse(res,0,'GET_ARTICLEURL_ERROR',err.message);
             return 
         }
    }

    async adminSelectDetailByPage(req, res, next)
    {
        try
        {
            
            if(!req.params.page||req.params.page=='')
            throw new Error('获取分页信息失败');
            let page = Number(req.params.page)-1;
            if(page<0)
            throw new Error('页码小于1');
            let data = await articleModol.find({},{"Article_title":1,"Article_createOn":1,"_id":0})
              .sort({"Article_sort":-1,"_id":-1}).skip(page*5).limit(5);
            unifyResponse(res,1,'SEARCH_ADMINARTICLEPAGE_SUCCESSFUL',data);
        }
        catch(err)
        {
            console.log(err.message);
            unifyResponse(res,0,'SEARCH_ADMINARTICLEPAGE_ERROR',err.message);
        }
    }

    async getAllPage(req, res, next)
    {
        try
        {
             let allNumber=await articleModol.count();
             unifyResponse(res,1,'GET_GETALLPAGE_SUCCESSFUL',allNumber);
        }
        catch(err)
        {
            console.log(err);
            unifyResponse(res,0,'GET_GETALLPAGE_ERROR',err.message);
            return 
        }
    }

    async selectArticleDetailBytitle(req, res, next)
    {
        try
        {
            if(!req.body.title||req.body.title=='')
            throw new Error('查询条件不足，标题为空或缺失');
            let title = req.body.title;
            let data = await articleModol.find({"Article_title":title},{"_id":0,"__v":0});
            unifyResponse(res,1,'GET_SELECTARTICLEDETAILBYTITLE_SUCCESS',data);
        }
        catch(err)
        {
            console.log(err);
            unifyResponse(res,0,'GET_SELECTARTICLEDETAILBYTITLE_ERROR',err.message);
            return 
        }
    }
    
    async updateArticleByTitle(req, res, next)
    {
         try{
            if(!req.body.title)
            throw new Error('标题不能为空');
            else if(!req.body.url)  
            throw new Error('url不能为空');
            else if(!req.body.type)
            throw new Error('文章类型不能为空');
            else if(!req.body.description)
            throw new Error('文章描述不能为空');
          }
          catch(err){
                console.log(err.message, err,"hahahaha");
                unifyResponse(res,0,'GET_ADDARTICLEPARAM_ERROR',err.message);
                return 
          }
          try{
           const article={
              Article_title: req.body.title,
              Article_url: req.body.url,
              Article_sort: Number(req.body.sort) || 0 ,
              Article_content: req.body.content || '' ,
              Article_type: req.body.type,
              Article_imgUrl: req.body.imgUrl || 'material-1.png',
              Article_description: req.body.description,
              Article_preContent: req.body.preContent
           }
           await articleModol.update({Article_title:req.body.title},article);
           console.log('更新成功');
           unifyResponse(res,1,'GET_ADDARTICLEPARAM_SUCCESS','更新成功');
           }
           catch(err)
           {
                 console.log(err);
                 unifyResponse(res,0,'INSERT_ERROR_PARAM','更新失败');
                 return 
           }
    }

    async deleteArticleByTitle(req, res, next)
    {
        try
        {
            if(!req.body.title)
            throw new Error('标题不能为空');
        }
        catch(err){
              unifyResponse(res,0,'GET_ARTICLETITLE_ERROR',err.message);
              return 
        }
        try
        {
            await articleModol.remove({Article_title:req.body.title});
            unifyResponse(res,1,'DELETE_ARTICLE_SUCCESS',"删除文章成功");
        }
        catch(err){
            unifyResponse(res,0,'DELETE_ARTICLE_ERROR',"删除文章失败");
            return 
        }
    }
}

export default new Article()